package com.marvels.financial.collection.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.marvels.common.constants.PageConstant;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.financial.collection.dao.CheckListQueryDao;
import com.marvels.financial.collection.dto.ReminderDTO;
import com.marvels.financial.collection.dto.UrgentDTO;
import com.marvels.financial.collection.service.CheckListQueryService;
import com.marvels.financial.consum.FinancialConsumerProductApi;
import com.marvels.financial.loan.dto.McptProductType;

@Service
public class CheckListQueryServiceImpl implements CheckListQueryService {

	@Autowired
	private CheckListQueryDao checkListQueryDao;
	@Autowired
	private FinancialConsumerProductApi consumerProductApi;
	
	public PageInfo<ReminderDTO> selectReminderListByPage(ReminderDTO reminderDTO) throws MarvelsBusinessException {
		if(null==reminderDTO) throw new MarvelsBusinessException("入催清单查询参数为空！");
		Integer pageNum = reminderDTO.getPageNo();
		if(pageNum==null||pageNum==0){
			pageNum =PageConstant.CURRPAGE; 
		}
		
		Integer pageSize = reminderDTO.getPageSize();
		if(pageSize==null||pageSize==0){
			pageSize = PageConstant.PAGESIZE;
		}
		
		PageHelper.startPage(pageNum, pageSize);
		
		List<ReminderDTO> reminderList = checkListQueryDao.selectReminderList(reminderDTO);
		
		PageInfo<ReminderDTO> pageInfo = new PageInfo<ReminderDTO>(reminderList);
		
		return pageInfo;
	}

	@SuppressWarnings("deprecation")
	public HSSFWorkbook findExcelWorkbook(ReminderDTO reminderDTO) throws MarvelsBusinessException {
		HSSFWorkbook wb = new HSSFWorkbook();
		List<ReminderDTO> reminderList = checkListQueryDao.selectReminderList(reminderDTO);
		HSSFSheet sheet = wb.createSheet("本日入催清单");
		HSSFRow row = sheet.createRow(0);
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
		HSSFCell cell = row.createCell((short) 0);
		cell.setCellValue("序号");
		cell.setCellStyle(style);
		cell = row.createCell((short) 1);
		cell.setCellValue("入催日期");
		cell.setCellStyle(style);
		cell = row.createCell((short) 2);
		cell.setCellValue("业务品种");
		cell.setCellStyle(style);
		cell = row.createCell((short) 3);
		cell.setCellValue("渠道");
		cell.setCellStyle(style);
		cell = row.createCell((short) 4);
		cell.setCellValue("客户姓名");
		cell.setCellStyle(style);
		cell = row.createCell((short) 5);
		cell.setCellValue("客户身份证号码");
		cell.setCellStyle(style);
		cell = row.createCell((short) 6);
		cell.setCellValue("当前还款期数");
		cell.setCellStyle(style);
		cell = row.createCell((short) 7);
		cell.setCellValue("应还款日期");
		cell.setCellStyle(style);
		cell = row.createCell((short) 8);
		cell.setCellValue("应还金额");
		cell.setCellStyle(style);
		cell = row.createCell((short) 9);
		cell.setCellValue("类型");
		cell.setCellStyle(style);
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		//写入实体数据 实际应用中这些数据从数据库得到，
		for (int i = 0; i < reminderList.size(); i++) {
			row = sheet.createRow((int) i + 1);
			// 创建单元格，并设置值
			cell = row.createCell((short) 0);
			cell.setCellValue(i + 1);
			cell = row.createCell((short) 1);
			if (reminderList.get(i).getReminderDate().toString() != null) {
				cell.setCellValue(dateFormat.format(reminderList.get(i).getReminderDate()));						
			}
		    cell = row.createCell((short) 2);
			cell.setCellValue(reminderList.get(i).getProductTypeName().toString());
			cell = row.createCell((short) 3);
			cell.setCellValue(reminderList.get(i).getChannel());
			
			cell = row.createCell((short) 4);
			cell.setCellValue(reminderList.get(i).getCustName());
			cell = row.createCell((short) 5);
			cell.setCellValue(reminderList.get(i).getCustIdNbr());
			cell = row.createCell((short) 6);
			cell.setCellValue(reminderList.get(i).getCurrTermNo().toString());
			cell = row.createCell((short) 7);
			cell.setCellValue(dateFormat.format(reminderList.get(i).getPaymentDate()).toString());						

			cell = row.createCell((short) 8);
			cell.setCellValue(reminderList.get(i).getPrincipalStaticAmt().doubleValue());
			cell = row.createCell((short) 9);
			 if (reminderList.get(i).getStatusType() == 0) {
				cell.setCellValue("全部逾期");
			}else
				cell.setCellValue("部分逾期");
		}
		return wb;
	}

	public PageInfo<UrgentDTO> selectUrgentList(UrgentDTO urgentDTO) throws MarvelsBusinessException {
		if(null==urgentDTO) throw new MarvelsBusinessException("出催清单查询参数为空！");
		Integer pageNum = urgentDTO.getPageNo();
		if(pageNum==null||pageNum==0){
			pageNum =PageConstant.CURRPAGE; 
		}
		
		Integer pageSize = urgentDTO.getPageSize();
		if(pageSize==null||pageSize==0){
			pageSize = PageConstant.PAGESIZE;
		}
		
		PageHelper.startPage(pageNum, pageSize);
		
		List<UrgentDTO> urgentList = checkListQueryDao.selectUrgentList(urgentDTO);
		
		PageInfo<UrgentDTO> pageInfo = new PageInfo<UrgentDTO>(urgentList);
		
		return pageInfo;
	}

	@SuppressWarnings("deprecation")
	public HSSFWorkbook findUrgentExcelWorkbook(UrgentDTO urgentDTO) throws MarvelsBusinessException {
		HSSFWorkbook wb = new HSSFWorkbook();
		List<UrgentDTO> list = checkListQueryDao.selectUrgentList(urgentDTO);
		HSSFSheet sheet = wb.createSheet("本日出催清单");
		HSSFRow row = sheet.createRow(0);
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
		HSSFCell cell = row.createCell((short) 0);
		cell.setCellValue("序号");
		cell.setCellStyle(style);
		cell = row.createCell((short) 1);
		cell.setCellValue("出催日期");
		cell.setCellStyle(style);
		cell = row.createCell((short) 2);
		cell.setCellValue("业务品种");
		cell.setCellStyle(style);
		cell = row.createCell((short) 3);
		cell.setCellValue("渠道");
		cell.setCellStyle(style);
		cell = row.createCell((short) 4);
		cell.setCellValue("客户姓名");
		cell.setCellStyle(style);
		cell = row.createCell((short) 5);
		cell.setCellValue("客户身份证号码");
		cell.setCellStyle(style);
		cell = row.createCell((short) 6);
		cell.setCellValue("当前还款期数");
		cell.setCellStyle(style);
		cell = row.createCell((short) 7);
		cell.setCellValue("应还款日期");
		cell.setCellStyle(style);
		cell = row.createCell((short) 8);
		cell.setCellValue("实际还款日期");
		cell.setCellStyle(style);
		cell = row.createCell((short) 9);
		cell.setCellValue("还款方式");
		cell.setCellStyle(style);
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		// 第五步，写入实体数据 实际应用中这些数据从数据库得到，
		for (int i = 0; i < list.size(); i++) {
			row = sheet.createRow((int) i + 1);
			// 第四步，创建单元格，并设置值
			cell = row.createCell((short) 0);
			cell.setCellValue(i + 1);
			cell = row.createCell((short) 1);
			if (list.get(i).getOutPutDate().toString() != null) {
				cell.setCellValue(dateFormat.format(list.get(i).getOutPutDate()));						
			}
		    cell = row.createCell((short) 2);
			cell.setCellValue(list.get(i).getProductTypeName());
			cell = row.createCell((short) 3);
			cell.setCellValue(list.get(i).getChannel());
			
			cell = row.createCell((short) 4);
			cell.setCellValue(list.get(i).getCustName());
			cell = row.createCell((short) 5);
			cell.setCellValue(list.get(i).getCustCardNo());
			cell = row.createCell((short) 6);
			cell.setCellValue(list.get(i).getCurrTermNo().toString());
			cell = row.createCell((short) 7);
			cell.setCellValue(dateFormat.format(list.get(i).getPaymentDate()).toString());						

			cell = row.createCell((short) 8);
			cell.setCellValue(dateFormat.format(list.get(i).getActPaymentDate()));
			cell = row.createCell((short) 9);
			 if ("1".equals(list.get(i).getPayType())) {
				cell.setCellValue("自动代扣");
			}else if ( "4".equals(list.get(i).getPayType()) ) {
				cell.setCellValue("主动还款");
			}else if ("6".equals(list.get(i).getPayType()) ) {
				cell.setCellValue("手工代扣");
//			}else if ("A004".equals(list.get(i).getPayType()) ) {
//				cell.setCellValue("自定义还款");
			}else if ("5".equals(list.get(i).getPayType()) ) {
				cell.setCellValue("下期还款");
//			} else if ("A007".equals(list.get(i).getPayType()) ) {
//				cell.setCellValue("代还提前结清");
//			}else if ("A008".equals(list.get(i).getPayType()) ) {
//			cell.setCellValue("代还款");
			}else if ("9".equals(list.get(i).getPayType()) ) {
				cell.setCellValue("支付宝还款");
			}else if ("10".equals(list.get(i).getPayType()) ) {
				cell.setCellValue("支付宝提前结清");
//			}else if ("A".equals(list.get(i).getPayType()) ) {
//				cell.setCellValue("支付宝二维码");
			}else if ("3".equals(list.get(i).getPayType()) ) {
				cell.setCellValue("提前结清");
			}else if("2".equals(list.get(i).getPayType())){
				cell.setCellValue("当期还款");
			}
		}
		return wb;
	}

	public Map<String, Object> findConditionList() throws MarvelsBusinessException {
		Map<String, Object> map = new HashMap<>();
		List<McptProductType> productList = consumerProductApi.mcptProductTypeInfo(new McptProductType());
		map.put("productList", productList);
		return map;
	}

	@Override
	public String selectReminderTotalSum(ReminderDTO reminderDTO) throws MarvelsBusinessException {
		return checkListQueryDao.selectReminderTotalSum(reminderDTO);
	}

	@Override
	public String selectUrgentTotalSum(UrgentDTO urgentDTO) throws MarvelsBusinessException {
		return checkListQueryDao.selectUrgentTotalSum(urgentDTO);
	}

}
